#include "setupgpsdialog.h"
#include "ui_setupgpsdialog.h"

SetupGPSDialog::SetupGPSDialog(QWidget *parent) : QDialog(parent), ui(new Ui::SetupGPSDialog)
{
    ui->setupUi(this);
    QStringList tmpStringList = getComPortList();
    int N = tmpStringList.size();
    for(int i = 0; i < N; ++i)
        ui->nameComboBox->addItem(tmpStringList[i], tmpStringList[i]);
    if(N == 0)
    {
        ui->nameComboBox->addItem("Не обнаружено подключение");
        ui->saveButton->setEnabled(false);
    }
    ui->baudRateComboBox->addItem("4800", 4800);
    ui->baudRateComboBox->addItem("9600", 9600);
    ui->baudRateComboBox->addItem("14400", 14400);
    ui->dataBitsComboBox->addItem("5", 5);
    ui->dataBitsComboBox->addItem("6", 6);
    ui->dataBitsComboBox->addItem("7", 7);
    ui->dataBitsComboBox->addItem("8", 8);
    ui->dataBitsComboBox->setCurrentIndex(3);
    ui->stopBitsComboBox->addItem("1", 1);
    ui->stopBitsComboBox->addItem("1,5", 15);
    ui->stopBitsComboBox->addItem("2", 2);
    ui->flowControlComboBox->addItem("Не используется", 1);
    ui->flowControlComboBox->addItem("Аппаратный", 2);
    ui->flowControlComboBox->addItem("Программный", 3);
    ui->parityControlComboBox->addItem("Не используется", 1);
    ui->parityControlComboBox->addItem("Нечётность", 2);
    ui->parityControlComboBox->addItem("Чётность", 3);
    ui->parityControlComboBox->addItem("Всегда 1", 4);
    ui->parityControlComboBox->addItem("Всегда 0", 5);
    setFixedSize(size());
}

SetupGPSDialog::~SetupGPSDialog()
{
    delete ui;
}

bool SetupGPSDialog::Result(SerialPortSettings &aSettings, QString &aName, bool &aStart)
{

    switch(ui->baudRateComboBox->itemData(ui->baudRateComboBox->currentIndex()).toInt())
    {
    case 4800:
        aSettings.BaudRate = AbstractSerial::BaudRate4800;
        break;
    case 9600:
        aSettings.BaudRate = AbstractSerial::BaudRate9600;
        break;
    case 14400:
        aSettings.BaudRate = AbstractSerial::BaudRate14400;
        break;
    default:
        aSettings.BaudRate = AbstractSerial::BaudRate4800;
    }

    switch(ui->dataBitsComboBox->itemData(ui->dataBitsComboBox->currentIndex()).toInt())
    {
    case 5:
        aSettings.DataBits = AbstractSerial::DataBits5;
        break;
    case 6:
        aSettings.DataBits = AbstractSerial::DataBits6;
        break;
    case 7:
        aSettings.DataBits = AbstractSerial::DataBits7;
        break;
    case 8:
        aSettings.DataBits = AbstractSerial::DataBits8;
        break;
    default:
        aSettings.DataBits = AbstractSerial::DataBits8;
    }

    switch(ui->stopBitsComboBox->itemData(ui->stopBitsComboBox->currentIndex()).toInt())
    {
    case 1:
        aSettings.StopBits = AbstractSerial::StopBits1;
        break;
    case 15:
        aSettings.StopBits = AbstractSerial::StopBits1_5;
        break;
    case 2:
        aSettings.StopBits = AbstractSerial::StopBits2;
        break;
    default:
        aSettings.StopBits = AbstractSerial::StopBits1;
    }

    switch(ui->flowControlComboBox->itemData(ui->flowControlComboBox->currentIndex()).toInt())
    {
    case 1:
        aSettings.FlowControl = AbstractSerial::FlowControlOff;
        break;
    case 2:
        aSettings.FlowControl = AbstractSerial::FlowControlHardware;
        break;
    case 3:
        aSettings.FlowControl = AbstractSerial::FlowControlXonXoff;
        break;
    default:
        aSettings.FlowControl = AbstractSerial::FlowControlOff;
    }

    switch(ui->parityControlComboBox->itemData(ui->parityControlComboBox->currentIndex()).toInt())
    {
    case 1:
        aSettings.Parity = AbstractSerial::ParityNone;
        break;
    case 2:
        aSettings.Parity = AbstractSerial::ParityOdd;
        break;
    case 3:
        aSettings.Parity = AbstractSerial::ParityEven;
        break;
    case 4:
        aSettings.Parity = AbstractSerial::ParityMark;
        break;
    case 5:
        aSettings.Parity = AbstractSerial::ParitySpace;
        break;
    default:
        aSettings.Parity = AbstractSerial::ParityNone;
    }
    aName = ui->nameComboBox->itemData(ui->nameComboBox->currentIndex()).toString();
    aStart = ui->startCheckBox->isChecked();
    return true;
}

QStringList SetupGPSDialog::getComPortList(void) const
{
    SerialDeviceEnumerator *sde;
    sde = SerialDeviceEnumerator::instance();
    return sde->devicesAvailable();
}
